<?php
/**
 * 支付宝 - 面对面支付集成
 * comfiger
 * 2017-02-22
 **/
namespace Xijie\Model;
use Think\Model;

class WeiXinModel extends Model{
	//定义表名称
	protected $tableName = 'paylog';
	
	//微信支付相关APPID和APPSECERT
	protected $AppID = "wx088ef024f4efc54b";
	protected $AppSecret = "3c840f94f5613a41c98d117629d3f449";
	//商户账号
	protected $ShopID = "1321010701";
	//安全密钥
	protected $ApiKey = "Wx1iL30ceb94ux32tBQ2c8O1mM75n6zp"; 
	//protected $ApiKey = "3c840f94f5613a41c98d117629d3f449";
	//回调地址
	protected $notify_url = "http://mamagy.com/xj/pay/weixin.html";
	
	/**
	 * 生成微信支付串
	 *
	 **/
	public function weixinPayString($goods_name, $order_sn, $total_fee, $trade_type='NATIVE'){
		$weiUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";
		$roundString = md5(time().rand(1000,999999));
		//获取IP
		$ip = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
		//生成订单时间
		$start = date('YmdHis', time());
		//订单失效时间
		$end = date('YmdHis', strtotime("+30 days")); //DateDiff("d",s_date,e_date);
		$paramArray = array(
			'appid' => "{$this->AppID}",
			'attach' => "嬷嬷公寓",
			'body' => $goods_name,
			'mch_id' => "{$this->ShopID}",
			'nonce_str' => $roundString,
			'notify_url' => "{$this->notify_url}",
			'out_trade_no' => $order_sn,
			'spbill_create_ip' => $ip,
			'total_fee' => $total_fee,
			'trade_type' => $trade_type,
		);
		
		$stringKey = $this->WeixinSign($paramArray);
		
		$sign = strtoupper(md5(htmlspecialchars_decode($stringKey)));
		
		$xmlString = "<xml>
					   <appid>{$this->AppID}</appid>
					   <attach><![CDATA[嬷嬷公寓]]></attach>
					   <body><![CDATA[{$goods_name}]]></body>
					   <mch_id>{$this->ShopID}</mch_id>
					   <nonce_str>{$roundString}</nonce_str>
					   <notify_url>{$this->notify_url}</notify_url>
					   <out_trade_no>{$order_sn}</out_trade_no>
					   <spbill_create_ip>{$ip}</spbill_create_ip>
					   <total_fee>{$total_fee}</total_fee>
					   <trade_type>{$trade_type}</trade_type>
					   <sign>{$sign}</sign>
					</xml>";
		$httpContent = $this->sendHttpPostByCurl($weiUrl ,$xmlString);	
		
		return $httpContent;
	}
	
	/**
	 * 微信签名
	 **/
	public function WeixinSign($array) {
		ksort ( $array );
		$string = "";
		while ( list ( $key, $val ) = each ( $array ) ) {
			if(!empty($val)){
				if($string){
					$string .= "&".$key."=".$val;
				}else{
					$string .= $key."=".$val;
				}
			}
		}
		$string .= "&key=". $this->ApiKey;
		
		return htmlspecialchars($string);
	}
	
	/**
	 * 请求POST
	 *
	 **/
	private function sendHttpPostByCurl($url, $content){
		//首先检测是否支持curl
		if (!extension_loaded("curl")) {   
			trigger_error("对不起，请开启curl功能模块！", E_USER_ERROR);
		}
		try{
			//构造xml
			$xmldata="<?xml version='1.0' encoding='UTF-8'?>" . $content;
			//初始一个curl会话
			$curl = curl_init();
			//设置url
			curl_setopt($curl, CURLOPT_URL, $url);
			//设置发送方式：
			curl_setopt($curl, CURLOPT_POST, true);
			//设置发送数据
			curl_setopt($curl, CURLOPT_POSTFIELDS, $xmldata);
			//设置返回数据为一个字符串格式值
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
			//设置返回值
			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
			//抓取URL并把它传递给浏览器
			$file_contents = curl_exec($curl);
			//关闭cURL资源，并且释放系统资源
			curl_close($curl);
		}catch(Exception $e){
			echo $e;
		}
		return $file_contents;
	}
}